/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import QtQuick 2.12
import org.ukui.tablet 1.0

AppIcon {
    id: item
    iconWidth: modelData.columnSpan * cellSize - cellContentMargin
    visible: modelData.type !== LauncherItem.Placeholder
    disableDelete: !modelManager.groupEditable(eduRoot.groupId)
    tabletAppIconDropShadowVisible: false
    eduAppIconOpacityMaskVisible: true
    deleteIconVisible: modelManager.groupEditable(eduRoot.groupId)
    deleteIconLeftMargin: (-deleteIconWidth/2)
    deleteIconTopMargin: (-deleteIconWidth/5)
    deleteIconSource: "qrc:/ui/images/icon_delete_default.svg"
    deleteIconSourceOnPressed: "qrc:/ui/images/icon_delete_click.svg"
    deleteIconWidth: 24 * Style.widthScaleFactor
    opacity: {
        if (modelData.type === LauncherItem.AppIcon) {
            if (listView.moving) return 1
            if (dragMousearea.pressed) return 0.5
        }
        return 1
    }
    appIconSource: {
        switch (modelData.type) {
        case LauncherItem.AppIcon: return "image://icons/" + modelData.icon
        case LauncherItem.AddIcon:
            if (dragMousearea.pressed) {
                return "qrc:/ui/images/list-add-click.svg";
            } else {
                return "qrc:/ui/images/list-add-default.svg";
            }
        default: return ""
        }
    }
    iconState: modelData.state
    anchors {
        horizontalCenter: parent.horizontalCenter;
        verticalCenter: parent.verticalCenter
    }
    onDeleteIconClicked: {
        modelManager.removeItem(itemRoot.visualIndex, itemRoot.pageIndex, groupId)
    }
    onIconStateChanged: {
        if (modelData.type === LauncherItem.AddIcon) {
            if (iconState === LauncherItem.Normal) {
                item.visible = true
                itemName.visible = true
                dragMousearea.enabled = true
            } else {
                item.visible = false
                itemName.visible = false
                dragMousearea.enabled = false
            }
        }
    }
}
